var sourceType = '';
var sizeType = [
	['compressed'],
	['original'],
	['compressed', 'original']
]
//多图上传公用mixins
export default{
	data(){
		return{
			hasFullFlag : true,
			albumList : [],
			upSuccImgArr : [],
			count: [1, 2, 3, 4, 5,6],
			countIndex: 5,
			upMultiSucImgNum:0,
			btnTit:'确认提交',
			btnDisabFlag:false,
		}
	},
	methods:{
		chooseImage(){
			if (this.albumList.length === 6) { return; }
			uni.showActionSheet({
				title:'请选择图片',
				itemList: ['从手机相册选择', '拍照'],
				success: (res)=> {
					if(res.tapIndex == 0){
						sourceType = 'album';
					}else if(res.tapIndex == 1){
						sourceType = 'camera';
					}
					// #ifdef APP-PLUS
						this.sculpture(res.tapIndex);
					// #endif
					// #ifdef MP-WEIXIN
					var that = this;
					wx.chooseImage({
						count:this.albumList.length + this.count[this.countIndex] > 6 ? 6 - this.albumList.length : this.count[this.countIndex],
						//sizeType: ['original', 'compressed'],
						sizeType:['original'],
						sourceType: [sourceType],
						success: res=>{
						  this.chooseImgCallBack(res);
						}
					})
					// #endif
				},
				fail: function(res) {
					console.log(res.errMsg)
				}
			});
		},
		sculpture(e){
			if (e == 1) {
				//相机
				switch (uni.getSystemInfoSync().platform) {
					case 'android':
						this.requestAndroidPermission('CAMERA',e);
						break;
					case 'ios':
						this.judgeIosPermissionCamera();
						break;
				}
				
			}else {
				//相册
				switch (uni.getSystemInfoSync().platform) {
					case 'android':
						this.requestAndroidPermission('READ_EXTERNAL_STORAGE',e);
						break;
					case 'ios':
						this.judgeIosPermissionPhotoLibrary();
						break;
				}
			}
		},
		// 上传多图
		Upload(filePath,hasSelLen){
			var field = {pathName:this.pathName,relativePathTag:1};
			var header = null;
			// #ifdef APP-PLUS || MP-WEIXIN
				var url = this.serverUrl + '/cosFile/cosUpload';
				header = {'Content-Type':'application/json;charset=UTF-8','Content-Type':'application/x-www-form-urlencoded','token':uni.getStorageSync('token')};
			// #endif
			// #ifdef H5
				var url = '/api/cosFile/cosUpload';
				header = {'token':uni.getStorageSync('token')};
			// #endif
			const upload_task = uni.uploadFile({
			    url: url,
			    filePath: filePath, //上传的文件本地地址    
				header:header,
			    name: 'file',
				formData:field,
			    //附近数据，这里为路径     
			    success: (res)=>{
					if(JSON.parse(res.data).code == 200){
						this.upMultiSucImgNum ++;
						// var filename = data.datas[0]; //存储地址 显示
						this.upSuccImgArr.push(JSON.parse(res.data).datas.filePath);
						if(this.upMultiSucImgNum == hasSelLen){
							this.upMultiSucImgNum = 0;
							this.btnDisabFlag = false;
							this.btnTit = '确认提交';
						}
						//console.log(this.upSuccImgArr)
					}else if(JSON.parse(res1.data).code == 1000){
						this.btnDisabFlag = false;
						this.btnTit = '确认提交';
						uni.showToast({
							title : '服务器错误',
							icon : 'none'
						});
					}
				}
			});
		},
		chooseImgCallBack(res){
			this.btnDisabFlag = true;
			this.btnTit = '图片上传中...';
			this.albumList = this.albumList.concat(res.tempFilePaths);
			for (let i = 0; i < res.tempFilePaths.length; i++) {
				this.Upload(res.tempFilePaths[i],res.tempFilePaths.length);
			}
			if(res.tempFilePaths.length == 6){
				this.hasFullFlag = false;
			}
		},
		previewImage(e){
			var current = e.target.dataset.src
			uni.previewImage({
				current: current,
				urls: this.albumList
			})
		},
		delect(index){
			uni.showModal({
				title: '提示',
				content: '是否要删除该图片',
				confirmColor:'red',
				success: (res)=> {
					if (res.confirm) {
						this.albumList.splice(index,1);
						this.upSuccImgArr.splice(index,1);
						this.hasFullFlag = true;
					}
				}
			});
		},
	}
}